#include "registrationdialog.h"
#include "ui_registrationdialog.h"

#include <QDebug>
#include <QDateTime>
#include <QFileDialog>
#include <QRegExpValidator>
#define INPUT_COUNT 5

RegistrationDialog::RegistrationDialog(int styleId,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RegistrationDialog)
{
    ui->setupUi(this);
    move(0,0);
    initialDisplayStyle(styleId);
//#ifdef BUILD_ARM
//    setGeometry(70,128,460,595);
//#endif
    //init voew
    QRegExp rxPositive("^[0-9A-Z]{5}$");
    QRegExpValidator *pPositiveReg = new QRegExpValidator(rxPositive, this);
    ui->Registration_lineEdit_1->setValidator(pPositiveReg);
    ui->Registration_lineEdit_2->setValidator(pPositiveReg);
    ui->Registration_lineEdit_3->setValidator(pPositiveReg);
    ui->Registration_lineEdit_4->setValidator(pPositiveReg);
    ui->Registration_lineEdit_5->setValidator(pPositiveReg);
    ui->Registration_lineEdit_6->setValidator(pPositiveReg);

    //init
    registrationStatus = tr("注册状态");

    serialNumber = "201701018888";
    expireTime = "2016-12-02";

    QDateTime time = QDateTime::currentDateTime();
    currentTime =  time.toString("yyyy-MM-dd");

    registrationString << "" << "" << "" << "" << "" << "";
    uDiskLocation = "";
}

void RegistrationDialog::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);
        ui->returnBtn->setGeometry(510,400,70,40);
        break;
    default:
        break;
    }
}

RegistrationDialog::~RegistrationDialog()
{
    delete ui;
}

void RegistrationDialog::on_returnBtn_clicked()
{
    this->hide();
}

void RegistrationDialog::updateView()
{
    //qDebug()<<"RegistrationDialog::updateView()";
    ui->register_label->setText(registrationStatus);
    ui->serialNum_label->setText(serialNumber);
    QString tmp = expireTime.left(4)+"-"+expireTime.mid(4,2)+"-"+expireTime.right(2);
    ui->expireTime_label->setText(tmp);
    ui->currentTime_label->setText(currentTime.left(10));

    ui->Registration_lineEdit_1->setText(registrationString[0]);
    ui->Registration_lineEdit_2->setText(registrationString[1]);
    ui->Registration_lineEdit_3->setText(registrationString[2]);
    ui->Registration_lineEdit_4->setText(registrationString[3]);
    ui->Registration_lineEdit_5->setText(registrationString[4]);
    ui->Registration_lineEdit_6->setText(registrationString[5]);

    ui->browse_lineEdit->setText(uDiskLocation);
}

void RegistrationDialog::changeLanguage()
{
    ui->retranslateUi(this);
}

void RegistrationDialog::on_browse_Btn_clicked()
{
    //打开浏览窗口

    QFileDialog dialog(this,"");
    dialog.resize(580,400);
    dialog.setAcceptMode(QFileDialog::AcceptOpen);
    dialog.setNameFilter("INI(*.ini)");
    dialog.setDirectory("/");

    QStringList files = dialog.selectedFiles();
    if(!files.isEmpty())
    {
        if(dialog.exec() == QFileDialog::Accepted)
        {
            qDebug()<<"ok";
            QStringList name = dialog.selectedFiles();
            QString line = name.join("");
            ui->browse_lineEdit->setText(line);
            //path = line;
        }
        else
        {
            qDebug()<<"close";
        }
    }
}

void RegistrationDialog::on_register_Btn_clicked()
{
    registrationString[0] = ui->Registration_lineEdit_1->text();
    registrationString[1] = ui->Registration_lineEdit_2->text();
    registrationString[2] = ui->Registration_lineEdit_3->text();
    registrationString[3] = ui->Registration_lineEdit_4->text();
    registrationString[4] = ui->Registration_lineEdit_5->text();
    registrationString[5] = ui->Registration_lineEdit_6->text();

    QString regCode = "";
    if(registrationString.size() == 6)
    {
        for(int i = 0;i < 6;i++)
        {
           regCode = regCode+ registrationString[i];
        }
        //qDebug() << "regCode:" << regCode;
    }

    //传递 注册码
    emit registrateSignal(regCode);
}

void RegistrationDialog::on_register_Btn_2_clicked()
{
    //传递 文件路径
    emit registrateFileSignal(ui->browse_lineEdit->text());
}



void RegistrationDialog::on_Registration_lineEdit_1_textChanged(const QString &arg1)
{
     if(arg1.count() == INPUT_COUNT)
     {
         ui->Registration_lineEdit_2->setFocus();
     }
}

void RegistrationDialog::on_Registration_lineEdit_2_textChanged(const QString &arg1)
{
    if(arg1.count() == INPUT_COUNT)
    {
        ui->Registration_lineEdit_3->setFocus();
    }
}

void RegistrationDialog::on_Registration_lineEdit_3_textChanged(const QString &arg1)
{
    if(arg1.count() == INPUT_COUNT)
    {
        ui->Registration_lineEdit_4->setFocus();
    }
}

void RegistrationDialog::on_Registration_lineEdit_4_textChanged(const QString &arg1)
{
    if(arg1.count() == INPUT_COUNT)
    {
        ui->Registration_lineEdit_5->setFocus();
    }
}

void RegistrationDialog::on_Registration_lineEdit_5_textChanged(const QString &arg1)
{
    if(arg1.count() == INPUT_COUNT)
    {
        ui->Registration_lineEdit_6->setFocus();
    }
}
